<?php

class Helpers {

    public static $url;

    static function init() {
        Helpers::$url = get_site_url();
        global $wpdb;
        $wpdb->listing_termmeta = $wpdb->prefix . 'listing_termmeta';
    }

    static function generateDropDown() {
        
    }
    
    static function get_base_url() {
        return Helpers::$url;
    }

    static function get_dashboard_url() {
        return Helpers::$url . "/usuario/escritorio/";
    }

    static function get_profile_url() {
        return Helpers::$url . "/usuario/perfil/";
    }
    
    static function get_user_register_url() {
        return Helpers::$url . "/usuario/registro-consumo/";
    }
    
    static function get_how_to_read_electricity() {
        return Helpers::$url . "/usuario/como-leer-electricidad/";
    }

    static function get_how_to_read_water() {
        return Helpers::$url . "/usuario/como-leer-agua/";
    }
    
    static function get_visualization_url($kind) {
        return Helpers::$url . "/usuario/visualizacion-" . $kind . "/";
    }

    static function get_good_practices_url() {
        return Helpers::$url . "/buenas-practicas/";
    }
    
    static function get_consumption_type_url($type) {
        return Helpers::$url . "/tipo-consumo/" . $type;
    }

    static function get_consumption_type_paginated_url($type, $page) {
        return Helpers::$url . "/tipo-consumo/" . $type . "/page/" . $page . "/";
    }
    
    static function get_consumption_type_filtered_url($type, $lapse) {
        return Helpers::$url . "/tipo-consumo/" . $type. "?plazo=" . $lapse;
    }

    static function get_consumption_type_paginated_filtered_url($type, $page, $lapse) {
        return Helpers::$url . "/tipo-consumo/" . $type . "/page/" . $page . "/?plazo=" . $lapse;
    }

    static function get_user_contact_url() {
        return Helpers::$url . "/usuario/contacto/";
    }

    static function get_contact_url() {
        return Helpers::$url . "/contacto/";
    }

    static function get_help_url() {
        return Helpers::$url . "/usuario/ayuda/";
    }

    static function get_forgot_url() {
        return Helpers::$url . "/recuperar-contrasena/";
    }
    static function get_faq_url(){
        return Helpers::$url."/preguntas-frecuentes/";
    }
    static function get_legal_url(){
        return Helpers::$url."/legal/";
    }

}

function get_the_post_thumbnail_src($img) {
    return (preg_match('~\bsrc="([^"]++)"~', $img, $matches)) ? $matches[1] : '';
}

function custom_retrieve_password($user_login) {
    global $wpdb, $wp_hasher;

    $user_login = sanitize_text_field($user_login);

    if (empty($user_login)) {
        return false;
    } else if (strpos($user_login, '@')) {
        $user_data = get_user_by('email', trim($user_login));
        if (empty($user_data))
            return false;
    } else {
        $login = trim($user_login);
        $user_data = get_user_by('login', $login);
    }

    do_action('lostpassword_post');


    if (!$user_data)
        return false;

    // redefining user_login ensures we return the right case in the email
    $user_login = $user_data->user_login;
    $user_email = $user_data->user_email;

    do_action('retreive_password', $user_login);  // Misspelled and deprecated
    do_action('retrieve_password', $user_login);

    $allow = apply_filters('allow_password_reset', true, $user_data->ID);

    if (!$allow)
        return false;
    else if (is_wp_error($allow))
        return false;

    $key = wp_generate_password(20, false);
    do_action('retrieve_password_key', $user_login, $key);

    if (empty($wp_hasher)) {
        require_once ABSPATH . 'wp-includes/class-phpass.php';
        $wp_hasher = new PasswordHash(8, true);
    }
    $hashed = $wp_hasher->HashPassword($key);
    $wpdb->update($wpdb->users, array('user_activation_key' => $hashed), array('user_login' => $user_login));

    $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
    $message .= network_home_url('/') . "\r\n\r\n";
    $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
    $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
    $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
    $message .= '<' . network_site_url("recuperar-contrasena?key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";

    if (is_multisite())
        $blogname = $GLOBALS['current_site']->site_name;
    else
        $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

    $title = sprintf(__('[%s] Password Reset'), $blogname);

    $title = apply_filters('retrieve_password_title', $title);
    $message = apply_filters('retrieve_password_message', $message, $key);
    if ($message && !wp_mail($user_email, $title, $message)) {
        return false;
    }

    return true;
}

function get_year_from_index($id) {
    switch ($id) {
        case 1:
            return "Enero";
            break;
        case 2:
            return "Febrero";
            break;
        case 3:
            return "Marzo";
            break;
        case 4:
            return "Abril";
            break;
        case 5:
            return "Mayo";
            break;
        case 6:
            return "Junio";
            break;
        case 7:
            return "Julio";
            break;
        case 8:
            return "Agosto";
            break;
        case 9:
            return "Septiembre";
            break;
        case 10:
            return "Octubre";
            break;
        case 11:
            return "Noviembre";
            break;
        case 12:
            return "Diciembre";
            break;
    }
}

function get_months_till_current_array($year) {
    $current_year = date('Y');

    if ($year >= $current_year) {
        $current_month = date('m');
    } else {
        $current_month = 12;
    }

    $array = array(
        "1" => "Enero",
        "2" => "Febrero",
        "3" => "Marzo",
        "4" => "Abril",
        "5" => "Mayo",
        "6" => "Junio",
        "7" => "Julio",
        "8" => "Agosto",
        "9" => "Septiembre",
        "10" => "Octubre",
        "11" => "Noviembre",
        "12" => "Diciembre",
    );

    return array_slice($array, 0, $current_month, $preserve_keys = true);
}

function get_graph_label_months($array) {
    $result = array();
    $i = 0;
    foreach ($array as $item) {
        $result[$i] = substr(get_year_from_index($item->month), 0, 3) . " " . substr($item->year, -2);
        $i++;
    }
    return $result;
}

function get_days_of_month($year, $month) {
    $result = 0;
    switch ($month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            $result = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            $result = 30;
            break;
        case 2:
            $result = (!($year % 4) && ($year % 100)) || !($year % 400) ? 29 : 28;
            break;
    }
    return $result;
}

?>